package servlets.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Category;
import pojo.Colors;
import pojo.Images;
import pojo.Product;
import pojo.Sizes;
import services.ServiceAdminProducts;

public class ServletViewProducts extends HttpServlet {

    ServiceAdminProducts pro = new ServiceAdminProducts();
    
    List <Sizes> psizes = new ArrayList();
    List <Colors> pcolors = new ArrayList();
    List <Images> pimgs = new ArrayList();
    Category pcat = new Category();
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            
            List<Product> prod = new ArrayList();
            
            
            prod = pro.getAllProducts();
      
            out.println("<tr><th>Name</th> <th>Price</th> <th>Amount</th> <th>Sizes</th> <th>Colors</th> <th>Gender</th> <th>Kids</th> <th>Image</th> <th>Brand</th> <th>Product desc.</th> <th>Category</th> <th></th> <th></th> </tr>");
             for (Product p : prod) {
           
                 psizes = p.getSizesList();
                 pcolors = p.getColorsList();
                 pimgs = p.getImagesList();
                 pcat = p.getCategoryId();
                 
             out.println("<tr><td>"+p.getName()+"</td> <td>"+p.getPrice()+"</td> <td>"+p.getAmount()+"</td>");
             out.println("<td>");
             for(Sizes s : psizes)
             {
                 out.print(s.getSize()+" ");
             }
             out.println("</td>");
             
             out.println("<td>");
             for(Colors col : pcolors)
             {
                 out.print(col.getColor()+" ");
             }
             out.println("</td>");
             
             out.println("<td>"+pcat.getGender()+"</td>");
             
             if(pcat.getKids() == 1)
                out.println("<td>Yes</td>");
             else
                 out.println("<td>No</td>");
                 
             out.println("<td>");
              for (Images ima : pimgs)
              {
              
                  out.println("<div id='my_popup"+ima.getId()+"' style='display:none;border:1px dotted gray;padding:.3em;position:fixed; width:600px; height:320px; left:50%; top:50%; z-index:999; margin-left:-300px; margin-top:-160px;'><img src='"+ima.getLocation()+"'alt='' height='320px' width='600px'></img></div>");
                out.print("<a href='"+ima.getLocation()+"' onClick=\" $('#dim_wrapper').toggle(); $('#dim_wrapper').animate({'opacity':0.6}, 700 ); show_popup('my_popup"+ima.getId()+"');  return false;\" >view</a>");
              }
             out.println("</td>");
             
             out.println("<td>"+p.getBrand()+"</td> <td>"+p.getDescription()+"</td> <td>"+pcat.getDescription()+"</td> <td> <a href='admin_editProduct.jsp?pid="+p.getId()+"'> Edit</a></td> <td><a href='admin_deleteProduct?pid="+p.getId()+"' onclick='return confirm(\"Are you sure you want to delete product?\");'> Delete</a></td> </tr>");
             
             
             out.println("<script>function show_popup(id) {\n" +
"			if (document.getElementById){\n" +
"				obj = document.getElementById(id);\n" +
"				if (obj.style.display == \"none\") {\n" +
"					obj.style.display = \"\";\n" +
"				}\n" +
"			}\n" +
"		}\n" +
"		function hide_popup(id){\n" +
"			if (document.getElementById){\n" +
"				obj = document.getElementById(id);\n" +
"				if (obj.style.display == \"\"){\n" +
"					obj.style.display = \"none\";\n" +
"				}\n" +
"			}\n" +
"		}</script>");
        }
                    
            /* TODO output your page here. You may use following sample code. */
         
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
